//
//  WXEmoticonView.swift
//  weibo
//
//  Created by 王旭 on 16/4/27.
//  Copyright © 2016年 王旭. All rights reserved.

//弹出表情键盘自定义的toolBar

enum EmoticonButtonType : Int {
    case Recent = 0
    case Default
    case Emoji
    case Lxh
}

import UIKit

class WXEmoticonView: UIStackView {
    
    //关联collectionView滚动的属性
    var selectedSection : Int = 0 {
        didSet{
            //获得选中按钮
            let button = viewWithTag(selectedSection) as! UIButton
            if button != selectedButton {
                button.selected = true
                selectedButton?.selected = false
                selectedButton = button
                
            }
        }
    }
    
    //记录toolBar选中的按钮
    var selectedButton : UIButton?
    
    //定义toolBar选择按钮的闭包
    var emotionToolBarClickClosure :((type:EmoticonButtonType)->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        //设置布局方式
        distribution = .FillEqually
        //添加子控件
        addChildButton("最近", imageNamed: "compose_emotion_table_left", type: .Recent)
        addChildButton("二寿", imageNamed: "compose_emotion_table_mid", type: .Default)
        addChildButton("Emoji", imageNamed: "compose_emotion_table_mid", type: .Emoji)
        addChildButton("浪小花", imageNamed: "compose_emotion_table_right", type: .Lxh)

    }

    //添加自按钮方法
    private func addChildButton(title:String,imageNamed:String,type:EmoticonButtonType) {
        let button = UIButton()
        //添加点击事件
        button.addTarget(self, action: "childButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        //设置tag
        button.tag = type.rawValue
        //设置文本
        button.setTitle(title, forState: UIControlState.Normal)
        //设置文本字体
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        //设置不同状态下的文本颜色
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)
        //设置不同状态下的背景颜色
        button.setBackgroundImage(UIImage(named: "\(imageNamed)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "\(imageNamed)_selected"), forState: .Selected)
        //添加到View上
        addArrangedSubview(button)
    }
    
    //toolBar按钮被点击的方法
   @objc private func childButtonClick(button:UIButton) {
        //如果点击当前按钮
        if selectedButton == button {
            return
        }
    
    //--------------------以下为测试代码-----------------
//    //如果选择其他按钮--放在闭包之前有BUG-
//    selectedButton?.selected = false
//    selectedButton = button
//    button.selected = true
    //--------------------以上为测试代码-----------------
    
    
        //执行闭包->collectionView滚动设置
        emotionToolBarClickClosure?(type: EmoticonButtonType(rawValue: button.tag)!)
    
    //如果选择其他按钮--放在闭包之前有BUG
            selectedButton?.selected = false
            selectedButton = button
            button.selected = true
    }

}
